<template>
  <div>
    <!-- 面包屑导航栏 start -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item class="index" :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>数据统计</el-breadcrumb-item>
      <el-breadcrumb-item>数据报表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 面包屑导航栏 end -->
    <!-- 卡片主体区 start -->
    <el-card>
      <div><el-button class="btn-often" icon="el-icon-upload2" @click="uploadData($event)" v-show="isShow">导出</el-button></div>
      <div id="main" style="width: 750px; height: 400px"></div>
    </el-card>
    <!-- 卡片主体区 end -->
  </div>
</template>

<script>
import * as echarts from 'echarts'
import btn from '../utils/btn'
// import _ from 'lodash'
export default {
  data() {
    return {
      uid: '',
      role: '',
      isShow: false,
      option: {
        title: {
          text: '用户统计',
          subtext: '角色',
          left: 'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{b} : {c} ({d}%)'
        },
        legend: {
          bottom: 10,
          left: 'center',
          data: []
        },
        series: [
          {
            type: 'pie',
            radius: '65%',
            center: ['50%', '50%'],
            selectedMode: 'single',
            data: [],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      },
      order: {
        title: {
          text: '订单统计',
          left: 'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: '单价为{b}的订单个数 : {c}单'
        },
        xAxis: {
          type: 'category',
          data: []
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: [],
            type: 'line'
          }
        ]
      }
    }
  },
  created() {},
  mounted() {
    this.getRole()
  },
  methods: {
    getRole() {
      const user = JSON.parse(window.sessionStorage.getItem('user'))
      this.uid = user.id
      this.role = user.role
      if (user.role === '超级管理员' || user.role === '管理员') {
        this.isShow = true
        // 获取用户后台数据
        this.getRoleType()
      } else {
        // 获取订单数据
        this.getOrders()
      }
    },
    async getOrders() {
      const res = await this.$http.get('orderGroup', {
        params: {
          uid: this.uid
        }
      })
      if (res.data) {
        var myChart = echarts.init(document.getElementById('main'))
        // 处理数据
        res.data.forEach((item) => {
          this.order.xAxis.data.push(item.price + '元/次')
          this.order.series[0].data.push(item.ispay)
        })
        myChart.setOption(this.order)
      } else {
        this.$message.error('获取数据失败！请重新操作')
      }
    },
    async getRoleType() {
      const res = await this.$http.get('roleGroup')
      console.log('数据', res)
      if (res.data) {
        var myChart = echarts.init(document.getElementById('main'))
        // 处理数据
        this.option.series[0].data = res.data.map((item) => {
          return { value: item.account, name: item.role }
        })
        // 处理图例
        this.option.legend.data = res.data.map((m) => {
          return m.role
        })
        myChart.setOption(this.option)
      } else {
        this.$message.error('获取数据失败！请重新操作')
      }
    },
    async uploadData(e) {
      btn.preBtnFocus(e)
      if (this.role === '超级管理员' || this.role === '管理员') {
        window.location.href = 'http://127.0.0.1:8081/uploadUsers'
      }
    }
  }
}
</script>

<style lang="less" scoped></style>
